import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store'
Vue.use(Router)
import Layout from '@/views/layout/Index'
import { getPermissionBymid } from '@/api/permission'
import { auth } from '@/assets/utils/auth'
const router = new Router({
  scrollBehavior: () => ({ y: 0 }),
  routes: [
    { path: '/login', component: () => import('@/views/login/index'), hidden: true, meta: { skip: true }}, // 登录页面
    // 欢迎页
    {
      path: '/',
      name: 'Home',
      component: Layout
    },
    // 模版页
    {
      path: '/template',
      name: 'Template',
      redirect: '/template/list',
      component: Layout,
      meta: { title: '模版管理', icon: 'example' },
      children: [
        {
          path: 'list',
          name: 'TemplateList',
          component: () => import('@/views/template/list'),
          meta: { title: '模版列表', icon: 'table' }
        },
        {
          path: 'add',
          name: 'TemplateAdd',
          component: () => import('@/views/template/addEdit'),
          meta: { title: '添加模版', icon: 'table' }
        },
        {
          path: 'edit',
          name: 'TemplateEdit',
          component: () => import('@/views/template/addEdit'),
          meta: { title: '修改模版', icon: 'table' }
        }
      ]
    },
    // 音乐页
    {
      path: '/music',
      name: 'Music',
      redirect: '/music/list',
      component: Layout,
      meta: { title: '音乐管理', icon: 'example' },
      children: [
        {
          path: 'list',
          name: 'MusicList',
          component: () => import('@/views/music/list'),
          meta: { title: '音乐列表', icon: 'table' }
        },
        {
          path: 'add',
          name: 'MusicAdd',
          component: () => import('@/views/music/addEdit'),
          meta: { title: '添加音乐', icon: 'table' }
        },
        {
          path: 'edit',
          name: 'MusicEdit',
          component: () => import('@/views/music/addEdit'),
          meta: { title: '修改音乐', icon: 'table' }
        }
      ]
    },
    // 附件图片页
    {
      path: '/picture',
      name: 'Picture',
      redirect: '/picture/list',
      component: Layout,
      meta: { title: '图片管理', icon: 'example' },
      children: [
        {
          path: 'list',
          name: 'PictureList',
          component: () => import('@/views/picture/list'),
          meta: { title: '图片列表', icon: 'table' }
        }
      ]
    },
    // 祝福语页
    {
      path: '/bless',
      name: 'Bless',
      redirect: '/bless/list',
      component: Layout,
      meta: { title: '祝福语管理', icon: 'example' },
      children: [
        {
          path: 'list',
          name: 'BlessList',
          component: () => import('@/views/bless/list'),
          meta: { title: '祝福语列表', icon: 'table' }
        },
        {
          path: 'add',
          name: 'BlessAdd',
          component: () => import('@/views/bless/addEdit'),
          meta: { title: '添加祝福语', icon: 'table' }
        },
        {
          path: 'edit',
          name: 'BlessEdit',
          component: () => import('@/views/bless/addEdit'),
          meta: { title: '编辑祝福语', icon: 'table' }
        }
      ]
    },
    // 权限页
    {
      path: '/permission',
      name: 'Permission',
      redirect: '/permission/list',
      component: Layout,
      meta: { title: '权限管理', icon: 'example' },
      children: [
        {
          path: 'list',
          name: 'PermissionList',
          component: () => import('@/views/permission/list'),
          meta: { title: '权限列表', icon: 'table' }
        },
        {
          path: 'add',
          name: 'PermissionAdd',
          component: () => import('@/views/permission/addEdit'),
          meta: { title: '添加权限', icon: 'table' }
        },
        {
          path: 'edit',
          name: 'PermissionEdit',
          component: () => import('@/views/permission/addEdit'),
          meta: { title: '编辑权限', icon: 'table' }
        }
      ]
    },
    // 用户管理页
    {
      path: '/user',
      name: 'User',
      redirect: '/user/list',
      component: Layout,
      meta: { title: '用户管理', icon: 'example' },
      children: [
        {
          path: 'list',
          name: 'UserList',
          component: () => import('@/views/user/list'),
          meta: { title: '用户列表', icon: 'table' }
        },
        {
          path: 'add',
          name: 'UserAdd',
          component: () => import('@/views/user/addEdit'),
          meta: { title: '添加用户', icon: 'table' }
        },
        {
          path: 'edit',
          name: 'UserEdit',
          component: () => import('@/views/user/addEdit'),
          meta: { title: '编辑用户', icon: 'table' }
        }
      ]
    },
    // 角色管理页
    {
      path: '/role',
      name: 'Role',
      redirect: '/role/list',
      component: Layout,
      meta: { title: '角色管理', icon: 'example' },
      children: [
        {
          path: 'list',
          name: 'RoleList',
          component: () => import('@/views/role/list'),
          meta: { title: '角色列表', icon: 'table' }
        },
        {
          path: 'add',
          name: 'RoleAdd',
          component: () => import('@/views/role/addEdit'),
          meta: { title: '添加角色', icon: 'table' }
        },
        {
          path: 'edit',
          name: 'RoleEdit',
          component: () => import('@/views/role/addEdit'),
          meta: { title: '编辑角色', icon: 'table' }
        },
        {
          path: 'bind',
          name: 'RoleBind',
          component: () => import('@/views/role/bind'),
          meta: { title: '角色绑定权限', icon: 'table' }
        },
        {
          path: 'allot',
          name: 'RoleAllot',
          component: () => import('@/views/role/allot'),
          meta: { title: '角色分配用户', icon: 'table' }
        }
      ]
    },
    // 非法页面
    {
      path: '/error',
      name: 'error',
      component: Layout,
      redirect: '/error/404',
      meta: { title: '错误页面', icon: 'example', skip: true },
      children: [
        {
          path: '601',
          name: 'unallowed',
          component: () => import('@/views/error/601'),
          meta: { title: '未授权', icon: 'table', skip: true }
        },
        {
          path: '404',
          name: 'unfind',
          component: () => import('@/views/error/404'),
          meta: { title: '404', icon: 'table', skip: true }
        }
      ]
    },
    { path: '*', redirect: '/error' }
  ]
})
router.beforeEach((to, from, next) => {
  // 权限验证
  var mid = store.getters.mid || sessionStorage.getItem('mid')
  if (mid) {
    getPermissionBymid({ menuid: mid }).then(data => {
      if (data.result === 1) {
        var authState = auth(data.data)
        var functionAuthState = authState.functionAuthState
        store.dispatch('setFunctionAuthState', functionAuthState)
      } else {
        store.dispatch('setFunctionAuthState', 0)
      }
    })
  }
  // 登录验证
  if (!to.meta.skip) {
    var sid = store.getters.sid || sessionStorage.getItem('sid')
    if (!sid) {
      next('/login')
    }
  }
  // 路由验证
  // if (!to.meta.skip) {
  //   var allAuth = store.getters.allAuth || sessionStorage.getItem('allAuth') ? JSON.parse(sessionStorage.getItem('allAuth')) : ''
  //   if (allAuth) {
  //     if (!allAuth.find(n => n === to.path)) next('/error/601')
  //   }
  // }
  next()
})
export default router
